package com.mxpio.erp.inventory.service.impl;

import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.inventory.entity.FeedPlanNotice;
import com.mxpio.erp.common.inventory.entity.FeedPlanNoticeLine;
import com.mxpio.erp.common.inventory.service.FeedPlanNoticeService;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.purchase.entity.Supply;
import com.mxpio.erp.common.workshop.entity.ManufactureOrder;
import com.mxpio.erp.common.workshop.entity.MoPickingNotice;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class FeedPlanNoticeServiceImpl extends BaseServiceImpl<FeedPlanNotice> implements FeedPlanNoticeService {
    @Override
    @Transactional
    public Result<? extends Notice> close(String noticeNo) {
        FeedPlanNotice notice = this.getById(FeedPlanNotice.class,noticeNo);
        if (!notice.getNoticeStatus().equals(NoticeStatusEnums.NoticeStatus.CREATE.getStatusCode())){
            return Result.error("仅开立状态时允许该操作");
        }
        notice.setNoticeStatus(NoticeStatusEnums.NoticeStatus.REJECT.getStatusCode());
        this.update(notice);
        //同时关闭对应领料通知单
        JpaUtil.linu(MoPickingNotice.class).equal("noticeNo",notice.getSourcePickingNoticeNo()).set("noticeStatus",NoticeStatusEnums.NoticeStatus.REJECT.getStatusCode()).update();
        return Result.OK(notice);
    }

    @Override
    @Transactional
    public Result<List<FeedPlanNotice>> listByIds(String noticeNos) {
        String[] split = noticeNos.split(",");
        List<String> noticeNoList = Stream.of(split).collect(Collectors.toList());
        List<FeedPlanNotice> list = JpaUtil.linq(FeedPlanNotice.class)
                .collect("noticeNo", FeedPlanNoticeLine.class, "noticeNo")
                .collect("pnCode", Supply.class, "pnCode")
                .collect("bizNo", ManufactureOrder.class, "bizOrderNo")
                .in("noticeNo", noticeNoList).list();
        return Result.OK(list);
    }
}
